//
//  WoDeWeiDianViewController.m
//  EJLX
//
//  Created by adc on 15/11/17.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "WoDeWeiDianViewController.h"
#import "WoDeWeiDianTableViewCell.h"
#import "MyShopVC.h"
//#import "DianPuSheZhiViewController.h"
#import "shouYinTaiViewController.h"
#import "WoDeShangPinViewController.h"
#import "SmellTableViewCell.h"
#import "GeRenJiShiViewController.h"
#import "ShoppingVC.h"
#import "WoDeWeiDian.h"

#import "WoDeFenDianViewController.h"
#import "ShangPinXiangQingViewController.h"
#import "GMDCircleLoader.h"

#import "FenXiangTableViewCell.h"
#import "FirstShouYinViewController.h"
#import "MyDianPuViewController.h"
@interface WoDeWeiDianViewController ()

@end

@implementation WoDeWeiDianViewController
-(void)viewWillAppear:(BOOL)animated
{
    [self request];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

     [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
    
        [HttpsManager requestGetWoDeWeiDianCompletion:^(FirstWoDeWeiDian *weiDian)
        {
            if ((weiDian!=nil))
            {
                self.dataAry=weiDian.resuleAry;
                _weiDian=weiDian;
                [table reloadData];
                
                
                
                
                UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE , 44)];
                view.backgroundColor=RGB_COLOR(0x00c49f);
                [self.view addSubview:view];
                UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-35, 10, 70, 24)];
                label.text=@"我的微店";
                label.textColor=[UIColor whiteColor];
                [view addSubview:label];
                
                
                
                
                
                
                UIView*bigdaView=[[UIView alloc]initWithFrame:CGRectMake(0,0, WIDE,170+3+50+60+10+10)];
                
                
                UIButton*but1=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30, 30)];
                [but1 setImage:[UIImage imageNamed:@"返回箭头1"] forState:UIControlStateNormal];
                [but1 addTarget:self action:@selector(butClick) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:but1];
                UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(0,0, WIDE,170)];
                view1.backgroundColor=RGB(240, 240, 240);
                [bigdaView addSubview:view1];
                
                UIButton*imageBut=[[UIButton  alloc]initWithFrame:CGRectMake(WIDE/2-50, 10,100,100)];
                imageBut.layer.cornerRadius = 50;
                imageBut.clipsToBounds = YES;
                //
                [imageBut sd_setBackgroundImageWithURL:[NSURL URLWithString:_weiDian.avatar] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"头像1"]];
                //[imageBut setImage:[UIImage imageNamed:@"头像1"] forState:UIControlStateNormal];
                //image.image=[UIImage imageNamed:@"头像"];
                [imageBut addTarget:self action:@selector(imageBut) forControlEvents:UIControlEventTouchUpInside];
                [view1 addSubview:imageBut];
                UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-50, 10+100+10,100, 20)];
                if ([_weiDian.shopName isKindOfClass:[NSNull class]])
                {
                    label1.text=@"一键连微店";
                    
                }
                else
                {
                    label1.text=_weiDian.shopName;
                    
                    
                }
                
                label1.textAlignment=NSTextAlignmentCenter;
                label1.textColor=[UIColor blackColor];
                [view1 addSubview:label1];
                UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-70, 10+100+10+10+15,140, 20)];
                if ([_weiDian.url isKindOfClass:[NSNull class]])
                {
                    label2.text=@"http://www.baidu.com";
                    
                    
                    
                }
                
                else
                {
                    label2.text=_weiDian.url;
                }
                [view1 addSubview:label2];
                UIView*view2=[[UIView alloc]initWithFrame:CGRectMake(0,170+3+50+3, WIDE, 60)];
                [bigdaView addSubview:view2];
                UIView*shouYinView=[[UIView alloc]initWithFrame:CGRectMake(0,170+3, WIDE, 50)];
                shouYinView.backgroundColor=[UIColor whiteColor];
                [bigdaView addSubview:shouYinView];
                UIImageView*shouYinImage=[[UIImageView alloc]initWithFrame:CGRectMake(WIDE/2-30,10, 30, 30)];
                shouYinImage.image=[UIImage imageNamed:@"O2O收银台1"];
                [shouYinView addSubview:shouYinImage];
                UILabel*shouYinLabel=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2,10,70, 30)];
                shouYinLabel.text=@"O2O收银台";
                shouYinLabel.textColor=[UIColor blackColor];
                shouYinLabel.font=[UIFont boldSystemFontOfSize:12];
                [shouYinView addSubview:shouYinLabel];
                UIButton*shouYinBut=[[UIButton alloc]initWithFrame:CGRectMake(0,170, WIDE, 60)];
                [shouYinBut addTarget:self action:@selector(shouYin) forControlEvents:UIControlEventTouchUpInside];
                [bigdaView addSubview:shouYinBut];
                
                for (int i=0;i<5;i++)
                {
                    NSArray*ary=[NSArray arrayWithObjects:@"店铺设置1",@"新增商品1",@"分店",@"商品管理1",@"店铺分享1", nil];
                    UIButton*but=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-150)/10+((WIDE-150)/5+30)*i,0,30,30)];
                    [but setImage:[UIImage imageNamed:ary[i]] forState:UIControlStateNormal];
                    [but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
                    but.tag=i;
                    [view2 addSubview:but];
                    NSArray*ary1=[NSArray arrayWithObjects:@"店铺设置",@"新增商品",@"我的分店",@"商品管理",@"店铺分享", nil];
                    UILabel*label3=[[UILabel alloc]initWithFrame:CGRectMake((WIDE-250)/10+((WIDE-250)/5+50)*i,30,50,30)];
                    label3.text=ary1[i];
                    label3.textAlignment = NSTextAlignmentCenter;
                    label3.font=[UIFont boldSystemFontOfSize:12];
                    [view2 addSubview:label3];
                    
                }
                
                
                
                table=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, WIDE, HEIGHT-64) style:UITableViewStylePlain];
                NSLog(@"hhhhhhh%f",WIDE);
                table.delegate=self;
                table.dataSource=self;
                table.rowHeight=150;
                [self.view addSubview:table];
                [table registerNib:[UINib nibWithNibName:@"WoDeWeiDianTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
                
                __weak WoDeWeiDianViewController *weakSelf = self;
                [table addPullToRefreshWithActionHandler:^{
                    NSLog(@"当进行下拉刷新执行该方法");
                    [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
                }];
                
                [table addInfiniteScrollingWithActionHandler:^{
                    [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
                }];
                
                //[self.view addSubview:bigdaView];
                table.tableHeaderView=bigdaView;

            }
            else
            {
                LoginVC*vc=[[LoginVC alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                  }];
    
   

//    
//    if (_dataAry.count==0)
//    {
//        [self.view addSubview:bigdaView];
//    }
    
    [GMDCircleLoader hideFromView:self.view animated:YES];

    // Do any additional setup after loading the view from its nib.
}
- (void)requestEnd{
    [table.pullToRefreshView stopAnimating];
    
    [table.infiniteScrollingView stopAnimating];
    
}
-(void)request
{
    //[GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
    
    [HttpsManager requestGetWoDeWeiDianCompletion:^(FirstWoDeWeiDian *weiDian)
     {
         if (weiDian!=nil)
         {
             self.dataAry=weiDian.resuleAry;
             _weiDian=weiDian;
             [table reloadData];
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
        
     }];
}
-(void)biaoTou
{
    
}
-(void)shouYin
{
    NSLog(@"22");
    FirstShouYinViewController*vc=[[FirstShouYinViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==table)
    {
        return 1;
    }
    if (tableView==fenXiangTable)
    {
        return 4;
    }
    return 3;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (tableView==table)
    {
        return _dataAry.count;
    }
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView==fenXiangTable)
    {
        
        FenXiangTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"fenXiangCell" forIndexPath:indexPath];
        NSArray*ary=[NSArray arrayWithObjects:@"发送到朋友圈",@"发送给好友",@"发送给朋友",@"发送给亲友", nil];
        cell.text.text=ary[indexPath.row];
        return cell;
        
    }
    if (tableView==table)
    {
        
        WoDeWeiDian*weiDian=_dataAry[indexPath.section];
        WoDeWeiDianTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
     
        [cell.tuPian sd_setImageWithURL:[NSURL URLWithString:weiDian.imgUrl] placeholderImage:[UIImage imageNamed:@"头像1"]];
        cell.backgroundColor=[UIColor whiteColor];
        cell.fenXiao.text=weiDian.title;
        
      
        if (weiDian.discounting)
        {
            cell.shouJia.text=[NSString stringWithFormat:@"%.2f",weiDian.discountPrice*0.01];
            
           
        }
        else
        {
            cell.shouJia.text=[NSString stringWithFormat:@"%.2f",weiDian.price*0.01];
        }
      
        NSString*str1=[NSString stringWithFormat:@"%.2f",weiDian.commision*0.01];
        cell.yongJin.text=str1;
        NSString*str2=[NSString stringWithFormat:@"%d",weiDian.stockNum];
        cell.kuCun.text=str2;
        UIButton*fenXiangBut=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-200)/8.f+(50+(WIDE-200)/4.f)*0,5,50,20)];
        [fenXiangBut setBackgroundImage:[UIImage imageNamed:@"分享.jpg"] forState:UIControlStateNormal];
        [fenXiangBut  addTarget:self action:@selector(fenXiangButClick:) forControlEvents:UIControlEventTouchUpInside];
        fenXiangBut .tag=indexPath.section;
        [cell.bigView addSubview:fenXiangBut];
        UIButton*bianJiBut=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-200)/8.f+(50+(WIDE-200)/4.f)*1,5,50,20)];
        [bianJiBut setBackgroundImage:[UIImage imageNamed:@"编辑.jpg"] forState:UIControlStateNormal];
        [bianJiBut addTarget:self action:@selector(bianJiButClick:) forControlEvents:UIControlEventTouchUpInside];
        bianJiBut.tag=indexPath.section;
        [cell.bigView addSubview:bianJiBut];

      
       // _butSelect=weiDian.selling;
        if (weiDian.selling)
        {
           // _butSelect=YES;
            UIButton*xiaJiaBut=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-200)/8.f+(50+(WIDE-200)/4.f)*2,5,50,20)];
            [xiaJiaBut setBackgroundImage:[UIImage imageNamed:@"下架.jpg"] forState:UIControlStateNormal];
            
            [xiaJiaBut addTarget:self action:@selector(xiaJiaButClick:) forControlEvents:UIControlEventTouchUpInside];
            xiaJiaBut.tag=indexPath.section;
            [cell.bigView addSubview:xiaJiaBut];
        }
        else
        {
            //_butSelect=NO;
            UIButton*shangJiaBut=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-200)/8.f+(50+(WIDE-200)/4.f)*2,5,50,20)];
            [shangJiaBut setBackgroundImage:[UIImage imageNamed:@"上架.jpg"] forState:UIControlStateNormal];
            [shangJiaBut addTarget:self action:@selector(shangJiaButClick:) forControlEvents:UIControlEventTouchUpInside];
            shangJiaBut.tag=indexPath.section;
            [cell.bigView addSubview:shangJiaBut];

        }
        UIButton*delectBut=[[UIButton alloc]initWithFrame:CGRectMake((WIDE-200)/8.f+(50+(WIDE-200)/4.f)*3, 5,50,20)];
        [delectBut setBackgroundImage:[UIImage imageNamed:@"删除.jpg"] forState:UIControlStateNormal];
        [delectBut addTarget:self action:@selector(delectButClick:) forControlEvents:UIControlEventTouchUpInside];
        delectBut.tag=indexPath.section;
        [cell.bigView addSubview:delectBut];
        
        
         cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    SmellTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"semllCell" forIndexPath:indexPath];
    NSArray*ary=[NSArray arrayWithObjects:@"品牌分销",@"自营商品",@"个人集市", nil];
    cell.label1.text=ary[indexPath.row];
    NSArray*ary1=[NSArray arrayWithObjects:@"无需进货发货，0成本做掌柜",@"支持各种渠道推广，支持担保交易，支持物流跟踪，支持退货退款",@"无需进货发货，0成本做掌柜", nil];
    cell.image.image=[UIImage imageNamed:ary[indexPath.row]];
    cell.label2.text=ary1[indexPath.row];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
    
    
    
}
//分享
-(void)fenXiangButClick:(UIButton*)sender
{
    NSLog(@"1");
    
}
//编辑
-(void)bianJiButClick:(UIButton*)sender
{
    NSLog(@"2");
}
//下架
-(void)xiaJiaButClick:(UIButton*)sender
{
    WoDeWeiDian*me=_dataAry[sender.tag];
    
    
    
    NSMutableArray*ary=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@"%d",me.productId], nil];
    if (me.selling)
    {

        [HttpsManager requestGetShangJiaShangPinProducts:ary requestUrl:@"rem" Competion:^(int errcode, NSString *errmsg)
         {
             NSLog(@" errcode %d",errcode);
             NSLog(@"errmsg %@",errmsg);
             SHOW_ALERT(errmsg);
             me.selling=!me.selling;
             
             [table reloadData];
         }];

    }
//    else{
//        
//        [HttpsManager requestGetShangJiaShangPinProducts:ary requestUrl:@"rem" Competion:^(int errcode, NSString *errmsg)
//         {
//             me.selling=!me.selling;
//             NSLog(@" errcode %d",errcode);
//             NSLog(@"errmsg %@",errmsg);
//             SHOW_ALERT(errmsg);
//             [table reloadData];
//         }];
//    }
    
    
}
//删除
-(void)delectButClick:(UIButton*)sender
{
        if (_dataAry.count>0) {
            WoDeWeiDian*me=_dataAry[sender.tag];
            
            NSMutableArray*ary=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@"%d",me.productId], nil];
            
            [HttpsManager requestGetShangJiaShangPinProducts:ary requestUrl:@"del" Competion:^(int errcode, NSString *errmsg)
             {
                 NSLog(@" errcode %d",errcode);
                 NSLog(@"errmsg %@",errmsg);
                 SHOW_ALERT(errmsg);
             }];
            UITableViewCell *cell = (UITableViewCell*)[[[sender superview]superview] superview];
            NSIndexPath *index = [table indexPathForCell:cell];
            [_dataAry removeObjectAtIndex:index.section];
         
            [table reloadData];

 }
   }
//上架
-(void)shangJiaButClick:(UIButton*)sender
{
    WoDeWeiDian*me=_dataAry[sender.tag];
    
    NSMutableArray*ary=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@"%d",me.productId], nil];
    
    [HttpsManager requestGetShangJiaShangPinProducts:ary requestUrl:@"add" Competion:^(int errcode, NSString *errmsg)
     {
         me.selling=!me.selling;
         NSLog(@" errcode %d",errcode);
         NSLog(@"errmsg %@",errmsg);
         SHOW_ALERT(errmsg);
         [table reloadData];
     }];

}
-(void)butClick:(UIButton*)sender
{
    
    switch (sender.tag)
    {
        case 0:
        {
//            DianPuSheZhiViewController*vc=[[DianPuSheZhiViewController alloc]init];
//            [self.navigationController pushViewController:vc animated:YES];
            MyDianPuViewController*vc=[[MyDianPuViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
        case 1:
        {
            NSLog(@"22");
            bigView=[[UIView alloc]initWithFrame:CGRectMake(50, 200, WIDE-100, 200)];
            bigView.backgroundColor=[UIColor whiteColor];
            [self.view addSubview:bigView];
            UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, bigView.frame.size.width, 40)];
            view.backgroundColor=[UIColor whiteColor];
            [bigView addSubview:view];
            UIButton*shanChuBut=[[UIButton alloc]initWithFrame:CGRectMake(view.frame.size.width-20, 2, 20, 20)];
            [shanChuBut setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
            //[shanChuBut setTitle:@"删除" forState:UIControlStateNormal];
            //[shanChuBut setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [shanChuBut addTarget:self action:@selector(shanChuBut) forControlEvents:UIControlEventTouchUpInside];
            [view addSubview:shanChuBut];
            UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(bigView.frame.size.width/2-30, 10, 60, 20)];
            label.text=@"新增商品";
            label.textColor=[UIColor blackColor];
            label.font=[UIFont boldSystemFontOfSize:13];
            [view  addSubview:label];
            smellTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 40, bigView.frame.size.width, bigView.frame.size.height-40-10) style:UITableViewStylePlain];
            smellTable.dataSource=self;
            smellTable.delegate=self;
            [bigView addSubview:smellTable];
            [smellTable registerNib:[UINib nibWithNibName:@"SmellTableViewCell" bundle:nil] forCellReuseIdentifier:@"semllCell"];
            
            
            
        }
            
            break;
        case 2:
        {
            WoDeFenDianViewController*vc=[[WoDeFenDianViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
        case 3:
        {
            WoDeShangPinViewController*vc=[[WoDeShangPinViewController alloc]init];
            vc.shopId=[NSString stringWithFormat:@"%d",_weiDian.shopId];
            [self.navigationController pushViewController:vc animated:YES];
        }
            
            break;
        case 4:
        {
            fenXiangView=[[UIView alloc]initWithFrame:CGRectMake(50, 150, 250,44*6)];
            fenXiangView.backgroundColor=[UIColor whiteColor];
            [self.view addSubview:fenXiangView];
            UILabel*fenLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,250, 44)];
            fenLabel.text=@"分享";
            fenLabel.textColor=[UIColor blackColor];
            fenLabel.textAlignment=NSTextAlignmentCenter;
            [fenXiangView addSubview:fenLabel];
            UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(220, 10, 20, 20)];
            [backBut setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
            [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
            [fenXiangView addSubview:backBut];
           fenXiangTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 44,250, 44*5) style:UITableViewStylePlain];
            fenXiangTable.delegate=self;
            fenXiangTable.dataSource=self;
            [fenXiangView addSubview:fenXiangTable];
            [fenXiangTable registerNib:[UINib nibWithNibName:@"FenXiangTableViewCell" bundle:nil] forCellReuseIdentifier:@"fenXiangCell"];
        }
            
            break;
        default:
            break;
    }
    
}
-(void)backBut
{
    [fenXiangView removeFromSuperview];
}
-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated

{
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //[tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView==smellTable)
    {
        if (indexPath.row==0)
        {
            ShoppingVC*vc=[[ShoppingVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }
        if (indexPath.row==2)
        {
            GeRenJiShiViewController*vc=[[GeRenJiShiViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (tableView==table)
    {
        ShangPinXiangQingViewController*vc=[[ShangPinXiangQingViewController alloc]init];
        WoDeWeiDian*woDe=_dataAry[indexPath.section];
       
        vc.productId=[NSString stringWithFormat:@"%d",woDe.productId];
        vc.shopId=[NSString stringWithFormat:@"%d",_weiDian.shopId];
                [self.navigationController pushViewController:vc animated:YES];
          }
       
        
    
   
}

-(void)shanChuBut
{
    [bigView removeFromSuperview];
}
-(void)imageBut
{
    NSLog(@"btn ");
    MyShopVC *myShop = [[MyShopVC alloc] init];
    
    myShop.shopId=[NSString stringWithFormat:@"%d",_weiDian.shopId];
    [self.navigationController pushViewController:myShop animated:YES];
//        NSString*str=[NSString stringWithFormat:@"%d",_weiDian.shopId];
//    if ([str length])
//    {
//   
// [self.navigationController pushViewController:myShop animated:YES];
//        NSLog(@"1111");
//
//    }
//    else
//    {
//         myShop.shopId=str;
//         NSLog(@"222");
//        [self.navigationController pushViewController:myShop animated:YES];
//    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//返回按钮
-(void)butClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
